package com.yaorange.web;

import com.github.pagehelper.PageInfo;
import com.yaorange.entity.Module;
import com.yaorange.entity.Role;
import com.yaorange.service.ModuleService;
import com.yaorange.service.RoleService;
import com.yaorange.vo.ZtreeVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/system/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private ModuleService moduleService;

    //带模糊查询的分页
    @GetMapping({"/page/{pageNum}/{pageSize}/{conditionRoleName}","/page/{pageNum}/{pageSize}"})
    @ResponseBody//将后台数据转成json给前端用
    public PageInfo<Role> getPage(@PathVariable Integer pageNum,@PathVariable Integer pageSize,
                                  @PathVariable(required = false) String conditionRoleName){
        PageInfo<Role> pageInfo=roleService.getPageByCondition(pageNum,pageSize,conditionRoleName);
        return pageInfo;
    }




    //新增
    @PostMapping("/addRole")
    @ResponseBody
    public Map<String,Object> addRole(@RequestBody Role role){
        int i=roleService.addRole(role);
        HashMap<String, Object> map = new HashMap<>();
        if(i>0){
            map.put("status",20000);
            map.put("msg","新增成功!!!");
        }else {
            map.put("status",30000);
            map.put("msg","新增失败！！！");
        }
        return map;
    }



    //删除
    @DeleteMapping("/deleteRole/{roleId}")
    @ResponseBody
    public HashMap<String,Object> removeDept(@PathVariable Integer roleId){
        Role role=roleService.getRole(roleId);
        HashMap<String, Object> hashMap = new HashMap<>();
        if(role==null){
            hashMap.put("msg","删除失败，该部门不存在");
            hashMap.put("status",30000);
            return hashMap;
        }
        //0代表可用，-1代表冻结，1代表已删除
        role.setRoleState(1);
        role.setUpdateTime(new Date());
        int i=roleService.updateRole(role);

        if(i>0){
            hashMap.put("status",20000);
            hashMap.put("msg","删除成功");
        }else {
            hashMap.put("status",30000);
            hashMap.put("msg","删除失败");
        }
        return hashMap;
    }



    //更新
    //查询更新部门的信息（判断是否存在）
    @GetMapping("/updateRole/{roleId}")
    public Role getDeptById(@PathVariable Integer roleId){
        Role role = roleService.getRole(roleId);
        return role;
    }

    //真正更新部门的操作
    @PutMapping("/updateRole")
    @ResponseBody
    public HashMap<String,Object> updateRole(@RequestBody Role role){
        int i = roleService.updateRole(role);
        HashMap<String, Object> hashMap = new HashMap<>();
        if (i>0){
            hashMap.put("status",20000);
            hashMap.put("msg","修改成功");
        }else {
            hashMap.put("status",1204);
            hashMap.put("msg","修改失败");
        }
        return hashMap;
    }






    @GetMapping("/ztree")
    public List<ZtreeVO> getZtreeList(){
        //获取所有的模块集合
        List<Module> moduleList = moduleService.getList();
        //将模块集合-->VO集合
        List<ZtreeVO> ztreeVOS = new ArrayList<>();
        for (Module module : moduleList) {
            //Module-->VO
            ZtreeVO ztreeVO = new ZtreeVO();
            ztreeVO.setId(module.getModuleId());
            ztreeVO.setpId(module.getParentId());
            ztreeVO.setName(module.getModuleRemark());
            ztreeVO.setChecked(false);
            ztreeVO.setOpen(true);

            ztreeVOS.add(ztreeVO);
        }
        //返回ztree的vo集合
        return ztreeVOS;
    }

    @GetMapping("/moduleIds/{roleId}")
    public List<Integer> getModuleIdsByRoleId(@PathVariable Integer roleId){
        //调用业务方法返回分页数据
        return roleService.getModuleIds(roleId);
    }

    @PutMapping("/roleAndModule/{roleId}")
    public Map<String,Object> updateModules(@PathVariable Integer roleId,@RequestBody List<Integer> moduleIds){
        int rows = roleService.updateModules(roleId,moduleIds);
        //准备返回消息对象
        Map<String,Object> map = new HashMap<>();
        if(rows == 0 || rows == moduleIds.size()){
            map.put("status",20000);
            map.put("msg","模块关联成功");
        }else{
            map.put("status",30000);
            map.put("msg","模块关联失败");
        }
        return map;
    }
}
